'''
cron jobs
'''
from google.appengine.ext import webapp
from iform_util import sync_compaction_test, sync_compaction_form, update_compaction_project_info
import logging, model, datetime
from google.appengine.api import memcache
from google.appengine.ext import db
from manage import remove_duplicate
from gae_db_manager import GaeDbManager
import sync
  
def remove_duplicates():
    logging.info('removing duplicates.')
    try:
        tbl = 'compaction_test'
        gdm = GaeDbManager(tbl)
        gdm.update_schema()
        logging.info('%s schema is updated.' % tbl)
    except:
        logging.warning('Failed to update schema for %s' % tbl)
    try:
        tbl = 'asphalt_test'
        gdm = GaeDbManager(tbl)
        gdm.update_schema()
        logging.info('%s schema is updated.' % tbl)
    except:
        logging.warning('Failed to update schema for %s' % tbl)
    try:
        remove_duplicate('compaction_test')
        logging.info('duplicates in compaction_test are removed.')
    except:
        pass
    try:
        remove_duplicate('asphalt_test')
        logging.info('duplicates in compaction_test are removed.')
    except:
        pass
class CronHandler(webapp.RequestHandler):
    def get(self):
        self.post()
    def post(self):
        action = self.request.get('action')
        logging.info('cron job action=%s' % action)
        if action=='sync_iform':
            sync.sync_all()
            self.response.out.write('sync_iform done.')
        elif action=='exe_failed_sync':
            sync.run_failed_sync()
            self.response.out.write('run failed sync is done.')
        elif action=='delete_duplicates':
            remove_duplicates()
#            self.response.out.write('removing duplicates.')
#            try:
#                tbl = 'compaction_test'
#                gdm = GaeDbManager(tbl)
#                gdm.update_schema()
#                self.response.out.write('%s schema is updated.' % tbl)
#            except:
#                logging.warning('Failed to update schema for %s' % tbl)
#            try:
#                tbl = 'asphalt_test'
#                gdm = GaeDbManager(tbl)
#                gdm.update_schema()
#                self.response.out.write('%s schema is updated.' % tbl)
#            except:
#                logging.warning('Failed to update schema for %s' % tbl)
#            try:
#                remove_duplicate('compaction_test')
#                self.response.out.write('duplicates in compaction_test are removed.')
#            except:
#                pass
#            try:
#                remove_duplicate('asphalt_test')
#                self.response.out.write('duplicates in compaction_test are removed.')
#            except:
#                pass
            
        